<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Association extends MY_Controller {
	
	function __construct() {
        parent::__construct();
        $this->load->model('user_model', '', TRUE);
		$this->load->model('society_model', '', TRUE);
        $this->load->model('association_model', '', TRUE);
        $this->load->model('news_model', '', TRUE);
		$this->load->model('announcement_model', '', TRUE);
        $this->load->model('photo_model', '', TRUE);
    }
	
    /**
     * 社联人员主页
     * @param $association_id
     */
	function association_index($association_id = '') {
		
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$data['current_menu_tab'] = MENU_TAB_A;
		$condition['association_id'] = $association_id;
		$temp_array = $this->association_model->search_by_condition($condition);
		foreach ($temp_array as $v) {
			$data['association'] = $v;
		}
		
		$temp_logo_path = $this->photo_model->get_thumb_by_user_and_category($association_id, "1");
		foreach ($temp_logo_path as $v) {
			$data['logo_path'] = $v;
		}
		
		$data['scene_path'] = $this->photo_model->get_thumb_by_user_and_category($association_id, "3");		
		$this->load->view('frontend/association/association_index', $data);
	}
	
	/**
	 * TODO
	 * Enter description here ...
	 */
	function about() {
		$condition['sort_view_count'] = DESC;
		$condition['offset'] = '';
		$condition['per_page'] = "5";
		$data['hot_society'] = $this->society_model->search_by_condition($condition);
		$data['current_menu_tab'] = MENU_TAB_B;
    	
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$data['html_title'] = MENU_TAB_B_NAME;
		$this->load->view('frontend/association/about', $data);
	}

	function recommond_news() {
		//新闻推荐
		$condition['sort_view_count'] = DESC;
		$condition['offset'] = '';
		$condition['per_page'] = 8;
		$data = $this->news_model->search_by_condition($condition);
		return $data;
	}
	
	function news_list($param = 0) {
		$data['recommend_news'] = $this->recommond_news();
		
		$data['current_menu_tab'] = MENU_TAB_D;
    	
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();

    	$condition = $this->_get_condition($param);
    	
    	$condition['is_published'] = IS_PUBLISHED;
		//获得社联新闻（cid == 2）
		$condition['category'] = ASSOCIATION_UCID;
		if ($param == 0) {
			$condition['sort_create_time'] = DESC;
		}
		
		//$data['association_news'] = $this->news_model->search_by_condition($condition);
		$data['association_news_count'] = $this->news_model->search_by_condition($condition, "", TRUE);
    	
		//---------------------------------- 分页开始 ------------------------------------//
        $this->load->library('pagination');
        $config['base_url'] = ASSOCIATION_NEWS_LIST_PATH . $param;
        $config['total_rows'] = $data['association_news_count'];
        $config['uri_segment'] = 5;
        $config['per_page'] = PER_PAGE; 
        //$config['num_links'] = 5;  //页码链接偏移量
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = "class='anchor_button' style='font-size: 80%;'";
        //$config['suffix'] = "#pagefocus";
        
        $this->pagination->initialize($config);

		$condition['per_page'] = PER_PAGE;
		$condition['offset'] = $this->uri->segment(5);
		
		$data['association_news'] = $this->news_model->search_by_condition($condition);
		//---------------------------------- 分页结束 ------------------------------------//
		
		$data['param'] = $param;
		
		$data['html_title'] = MENU_TAB_D_NAME;
		$this->load->view('frontend/association/news_list', $data);
	}
	
	function news_detail($id) {
		$condition['is_published'] = IS_PUBLISHED;
    	$condition['category'] = ASSOCIATION_UCID;
		$condition['id'] = $id;
		$data['news_detail'] = $this->news_model->search_by_condition($condition, TRUE);
		if (empty($data['news_detail'])) { show_404(); }
		
		$data['news_detail']['poster_name']
			= $this->association_model->_get_association_name_by_employee_id($data['news_detail']['poster_id']);
		//echo "<pre>";
		//print_r($data['news_detail']);
		//return ;
		
		$data['recommend_news'] = $this->recommond_news();
		
		$data['current_menu_tab'] = MENU_TAB_D;
    	
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
    	
		$nearest_condition['category'] = ASSOCIATION_UCID;
		$nearest_condition['sort_create_time'] = DESC;
		$data['nearest_id'] = $this->_get_nearest_id($id, $nearest_condition, NEWS_TYPE);
		
		$data['html_title'] = $data['news_detail']['title'];
		$this->load->view('frontend/association/news_detail', $data);
	}
	
	/**
	 * 获得当前分类内容的上一篇或下一篇的ID
	 * @param $id
	 * @param $condition
	 * @param $type
	 */
	function _get_nearest_id($id, $condition, $type) {
		switch ($type) {
			case NEWS_TYPE:
				$temp = $this->news_model->search_by_condition($condition, FALSE);
				break;
			case ANNOUNCEMENT_TYPE:
				$temp = $this->announcement_model->search_by_condition($condition, FALSE);
				break;
		}
		
		foreach ($temp as $k => $v) {
			$all_id[] = $v['id'];
			if ($v['id'] == $id) {
				$cur_key = $k;
				$prev_key = $k - 1;
				$next_key = $k + 1;
			}
		}
		if (array_key_exists($prev_key, $all_id)) {
			$nearest_id['prev_id'] = $all_id[$prev_key];
		}
		if (array_key_exists($next_key, $all_id)) {
			$nearest_id['next_id'] = $all_id[$next_key];
		}
		return (isset($nearest_id)) ? $nearest_id : '';
	}
	
	function _get_condition($param) {
		
    	if ($param != 0) {
			if ($param == 1) {
				$condition['sort_create_time'] = ASC;
			} elseif ($param == 2){
				$condition['sort_create_time'] = DESC;
			} elseif ($param == 3) {
				$condition['sort_view_count'] = DESC;
			} else {
				$condition['sort_view_count'] = ASC;
			}
		}
		if (!empty($condition)) {
			return $condition;
		}
    }
    
    function ajax_announcement_detail() {
    	$condition['is_published'] = IS_PUBLISHED;
    	$condition['category'] = ASSOCIATION_UCID;
		$condition['id'] = $this->input->post('id');
		$data = $this->announcement_model->search_by_condition($condition, TRUE);
		
    	if (empty($data)) { show_404(); }
    	
    	$data['create_time'] = date("Y年m月d日", $data['create_time']);
    	
    	//poster info
		$data['author']['name'] = ASSOCIATION_XCB_NAME . ' ' . $this->association_model->_get_association_name_by_employee_id($data['poster_id']);
		$data['author']['href'] = ASSOCIATION_ABOUT_PATH . ASSOCIATION_XCB_CID;
		$data['author']['title'] = '关于社联' . ASSOCIATION_XCB_NAME;
		
		$nearest_condition['category'] = ASSOCIATION_UCID;
		$nearest_condition['sort_create_time'] = DESC;
		$data['nearest_id'] = $this->_get_nearest_id($condition['id'], $nearest_condition, ANNOUNCEMENT_TYPE);
		
		$this->output->set_output(json_encode($data));
	}
    
	function ajax_update_news_view_count() {
		$news_id = $this->input->post('news_id');
		if ($this->_update_cookie('news_read', $news_id)) {
			$this->news_model->update_view_count($news_id);
		}
    }
    
}